package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: liangWeiQUQ
 * @Description: 获取会员统计数据
 * @DateTime: 2021/3/8 17:34
 **/
@DubboService
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * @Author: 传闻中的梁大侠
     * @Date: 17:36 2021/3/8
     * @Parms [months]
     * @ReturnType: java.util.List<java.lang.Integer>
     * @Description: 获取会员统计数据
     */
    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            Integer count = memberDao.findByMonth(month+".31");
            memberCount.add(count);
        }
        return memberCount;
    }
    /**
     * @Author: 传闻中的梁大侠
     * @Date: 17:23 2021/3/8
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查看套餐的统计数据
     */
    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> setmealCount = setmealDao.getCountSetmeal();
        //放入集合中去
        resultMap.put("setmealCount", setmealCount);
        //建立一个新的集合存放遍历套餐名称
        List<String> setmealNames = new ArrayList<>();
        //遍历查询结果中的套餐名称
        for (Map<String, Object> map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);
        }
        resultMap.put("setmealNames", setmealNames);
        return resultMap;
    }
    /**
     * @Author: 传闻中的梁大侠
     * @Date: 11:48 2021/3/9
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 会员统计
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> resultMap = new HashMap<>();
        //创建日期类
        Calendar today = Calendar.getInstance();
        //将所有的时间清空为0
        today.set(Calendar.HOUR_OF_DAY, 0);//时
        today.set(Calendar.MINUTE, 0);//分
        today.set(Calendar.SECOND, 0);//秒
        today.set(Calendar.MILLISECOND, 0);//毫米
        //将周一设置为本周的第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);

        //本周的第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //本周的最后一天
        Calendar weekLastDay = (Calendar) today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        //本月的第一天
        Calendar monthFirstDay = (Calendar) today.clone();
        //将本月的第一天设置为空
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);
        //本月最后一天
        Calendar monthLastDay = (Calendar) today.clone();
        //调用api得到本月的最后一天
        int actualMaximum = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH, actualMaximum);
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //当天
        String todayStr = sdf.format(today.getTime());
        String weekFirstDayStr = sdf.format(weekFirstDay.getTime());
        String weekLastDayStr = sdf.format(weekLastDay.getTime());
        String monthFirstDayStr = sdf.format(monthFirstDay.getTime());
        String monthLastDayStr = sdf.format(monthLastDay.getTime());

        //返回结果数据
        resultMap.put("reportDate", todayStr);
        resultMap.put("totalMember", memberDao.countTotal());
        resultMap.put("todayNewMember", memberDao.countRangRegTime(todayStr, todayStr));
        resultMap.put("thisWeekNewMember", memberDao.countRangRegTime(weekFirstDayStr, weekLastDayStr));
        resultMap.put("thisMonthNewMember", memberDao.countRangRegTime(monthFirstDayStr, monthLastDayStr));

        resultMap.put("todayOrderNumber", orderDao.countByOrderDateAndOrderStatus(todayStr, todayStr, null));
        resultMap.put("todayVisitsNumber", orderDao.countByOrderDateAndOrderStatus(todayStr, todayStr, Order.ORDERSTATUS_YES));

        resultMap.put("thisWeekOrderNumber", orderDao.countByOrderDateAndOrderStatus(weekFirstDayStr, weekLastDayStr, null));
        resultMap.put("thisWeekVisitsNumber", orderDao.countByOrderDateAndOrderStatus(weekFirstDayStr, weekLastDayStr, Order.ORDERSTATUS_YES));

        resultMap.put("thisMonthOrderNumber", orderDao.countByOrderDateAndOrderStatus(monthFirstDayStr, monthLastDayStr, null));
        resultMap.put("thisMonthVisitsNumber", orderDao.countByOrderDateAndOrderStatus(monthFirstDayStr, monthLastDayStr, Order.ORDERSTATUS_YES));
        resultMap.put("hotSetmeal", setmealDao.findHotSetmeal());
        return resultMap;
    }
}
